import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';
import 'package:largefriends/res/lf_double_ext.dart';
import 'package:largefriends/res/lf_string_ext.dart';
import 'package:largefriends/utils/lf_scroll_behavior.dart';
import 'package:largefriends/widgets/dialog_action_tip.dart';
import 'package:largefriends/widgets/lf_app_bar.dart';
import 'package:largefriends/widgets/lf_single_picker.dart';

import '../../../config/lf_type_config.dart';
import '../../../res/lf_color.dart';
import '../../../res/lf_fonts.dart';
import '../../../res/lf_text_style.dart';
import '../../../res/strings/lf_strings.dart';
import '../../../utils/lf_dialog_utils.dart';
import '../../../widgets/bottom_dialog_widget.dart';
import '../lf_change_username/lf_change_username_logic.dart';
import 'lf_edit_basic_logic.dart';

class LfEditBasicPage extends StatefulWidget {
  const LfEditBasicPage({super.key});

  @override
  State<LfEditBasicPage> createState() => _LfEditBasicPageState();
}

class _LfEditBasicPageState extends State<LfEditBasicPage> {
  final logic = Get.put(LfEditBasicLogic());

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
        child: Scaffold(
          appBar: lfAppBar(
              leadingCallback: () {
                Get.back();
              },
              title: strBasic.tr),
          body: GetBuilder<LfEditBasicLogic>(
            builder: (logic) {
              return ScrollConfiguration(
                behavior: LfScrollBehavior(),
                child: CustomScrollView(
                  slivers: [
                    _textContent(
                        title: strUserName.tr,
                        content: logic.authService.currentAccount!.username,
                        click: () {
                          toChangeUsername();
                        }).sliverBox,
                    _textContent(
                      title: strBirthday.tr,
                      content: DateFormat('MMMM dd, yyyy').format(
                        DateTime.fromMillisecondsSinceEpoch(
                            logic.authService.currentAccount!.birthday! * 1000),
                      ),
                      click: () {
                        if (logic.authService.currentAccount!.changedBirthday <
                            1) {
                          showActionTips(
                              tips: strChangeBirthdayTips.tr,
                              actionButtonTip: strSure.tr,
                              action: () {
                                _birthdaySheet((date) {
                                  if (date != null) {
                                    if (DateFormat('yyyy-MM-dd').format(date) !=
                                        DateFormat('yyyy-MM-dd').format(
                                            DateTime.fromMillisecondsSinceEpoch(
                                                logic
                                                        .authService
                                                        .currentAccount!
                                                        .birthday! *
                                                    1000))) {
                                      logic.authService.updateUserProfile(
                                        data: {
                                          "user[birthday]":
                                              DateFormat('yyyy-MM-dd').format(
                                            date,
                                          ),
                                        },
                                      );
                                    }
                                  }
                                });
                              });
                        } else {
                          showActionTips(
                              tips: strAlreadyChangedTips.tr,
                              actionButtonTip: strOK.tr,
                              action: () {},
                              showCancel: false);
                        }
                      },
                    ).sliverBox,
                    _textContent(
                        title: strGender.tr,
                        content: TypeConfig().getValueByKey(
                            LfConfigTypeEnum.gender,
                            logic.authService.currentAccount!.gender),
                        click: () {
                          if (logic.authService.currentAccount!.changedGender <
                              1) {
                            showActionTips(
                                tips: strChangeGenderTips.tr,
                                actionButtonTip: strSure.tr,
                                action: () {
                                  showLfSinglePicker(
                                      configTypeEnum: LfConfigTypeEnum.gender,
                                      value: logic
                                          .authService.currentAccount!.gender,
                                      saveCallback: (value) {
                                        if (value !=
                                            logic.authService.currentAccount!
                                                .gender) {
                                          logic.authService.updateUserProfile(
                                            data: {
                                              "user[gender]": value,
                                            },
                                          );
                                        }
                                      });
                                });
                          } else {
                            showActionTips(
                                tips: strAlreadyChangedTips.tr,
                                actionButtonTip: strOK.tr,
                                action: () {},
                                showCancel: false);
                          }
                        }).sliverBox,
                  ],
                ),
              );
            },
          ),
        ),
        onWillPop: () async {
          Get.back();
          return false;
        });
  }

  Widget _textContent(
      {required String title,
      required String content,
      required Function click}) {
    return Padding(
      padding: EdgeInsets.only(bottom: 20.h),
      child: InkWell(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                16.w.sbw,
                Expanded(
                    child: Text(
                  title,
                  style: lfTextStyle(
                      sp: 17.sp, fontFamily: fontBold, color: color514F62),
                )),
                Icon(Icons.chevron_right,color: color514F62,),
                16.w.sbw,
              ],
            ),
            8.h.sbh,
            Padding(
              padding: EdgeInsets.symmetric(horizontal: 16.w),
              child: Text(
                content.trim().replaceBreak.isNullOrEmpty == false
                    ? content.trim().replaceBreak ?? ""
                    : "",
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
                style: lfTextStyle(
                  sp: 16.sp,
                  fontFamily: fontRegular,
                ),
              ),
            )
          ],
        ),
        onTap: () {
          click.call();
        },
      ),
    );
  }

  _birthdaySheet(Function(DateTime?) confirm) {
    DateTime? dateTime;
    showLfBottomDialogWidget(
        onConfirm: () {
          hideSheet();
          confirm.call(dateTime);
        },
        children: [
          Expanded(
              child: CupertinoDatePicker(
            onDateTimeChanged: (date) {
              dateTime = date;
            },
            initialDateTime: DateTime.fromMillisecondsSinceEpoch(
                logic.authService.currentAccount!.birthday!),
            minimumDate: DateTime(DateTime.now().year - 99,
                DateTime.now().month, DateTime.now().day),
            maximumDate: DateTime(DateTime.now().year - 40,
                DateTime.now().month, DateTime.now().day - 1),
            mode: CupertinoDatePickerMode.date,
            minimumYear: DateTime.now().year - 99,
            maximumYear: DateTime.now().year - 40,
            backgroundColor: Colors.white,
          ))
        ]);
  }
}
